const app = getApp();
const ajax = app.myRequest()
import Dialog from '../../../miniprogram_npm/@vant/weapp/dialog/dialog'
Page({
	data: {},

	onLoad: function (options) {
		const scene = decodeURIComponent(options.scene)
		let order_id = scene.split("&amp;")[0];
		let type = scene.split('&amp;')[1];
		let gymnasium_id = scene.split('&amp;')[2];
		this.setData({ order_id, type, gymnasium_id })
	},
	
	onShow() {
		this.getName()
	},

	getName() {
		var _this = this
		wx.getSetting({
			success: function (res) {
				if (res.authSetting['scope.userInfo']) {
					ajax.getRequest('/User/profile').then((res) => {
						var gymnasium = res.data.data
						if (_this.data.type == 1) {
							_this.relation(gymnasium)
						} else if (_this.data.type == 2) {
							_this.RewardCard(gymnasium)
						}
						_this.setData({ gymnasium })
					})
				} else {
					wx.navigateTo({ url: '/pages/login/SignIn/index' })
				}
			}
		})
	},

	relation(gymnasium) {
		var Venue_id = this.data.gymnasium_id
		var _this = this
		if (!gymnasium.gymnasium && !gymnasium.reward) {
			Dialog.alert({ message: '您不是场馆或店员' }).then(() => {
				wx.switchTab({ url: '/pages/personal/my/index' })
			})
		} else if (gymnasium.gymnasium) {
			if (gymnasium.gymnasium.id == Venue_id) {
				if (gymnasium.gymnasium.status == 0) {
					this.setData({ content: '你的场馆正在审核，请审核通过后在核销会员卡' })
				} else if (gymnasium.gymnasium.status == 1) {
					var order_id = _this.data.order_id
					var gymnasium_id = gymnasium.gymnasium.id
					var type = _this.data.type
					ajax.postRequest('/Order/verification', { order_id, type, gymnasium_id }).then((res) => {
						if (res.data.code == 1) {
							_this.setData({ content: '会员卡核销成功' })
						} else {
							_this.setData({ content: res.data.msg })
						}
					})
				} else if (gymnasium.gymnasium.status == 2) {
					this.setData({ content: '您的场馆已禁用，暂时无法核销会员卡' })
				} else if (gymnasium.gymnasium.status == 3) {
					this.setData({ content: '您的场馆已驳回，暂时无法核销会员卡' })
				}
			} else {
				this.setData({ content: '请出示本店二维码' })
			}
		} else if (gymnasium.staff) {
			if (gymnasium.staff.gymnasium_id == Venue_id) {
				if (gymnasium.staff.status == 0) {
					this.setData({ content: '你的店员身份正在审核中' })
				} else if (gymnasium.staff.status == 1) {
					var order_id = _this.data.order_id
					var gymnasium_id = gymnasium.staff.gymnasium_id
					var type = _this.data.type
					ajax.postRequest('/Order/verification', { order_id, type, gymnasium_id }).then((res) => {
						if (res.data.code == 1) {
							_this.setData({ content: '会员卡核销成功' })
						} else {
							_this.setData({ content: res.data.msg })
						}
					})
				} else if (gymnasium.staff.status == 2) {
					this.setData({ content: '您的店员身份已禁用' })
				} else if (gymnasium.staff.status == 3) {
					this.setData({ content: '您的店员身份已驳回' })
				}
			} else {
				this.setData({ content: '请出示本店二维码' })
			}
		}
	},

	// 奖励卡核销
	RewardCard(gymnasium) {
		var _this = this
		if (!gymnasium.gymnasium && !gymnasium.staff) {
			Dialog.alert({ message: '您不是场馆' }).then(() => {
				wx.switchTab({ url: '/pages/personal/my/index' })
			})
		} else if (gymnasium.gymnasium) {
			if (gymnasium.gymnasium.status == 0) {
				Dialog.alert({ message: '你的场馆正在审核中' }).then(() => {
					wx.switchTab({ url: '/pages/personal/my/index' })
				})
			}
			else if (gymnasium.gymnasium.status == 1) {
				var order_id = _this.data.order_id
				var gymnasium_id = gymnasium.gymnasium.gymnasium_id
				var type = _this.data.type
				ajax.postRequest('/Order/verification', { reward_id: order_id, type, gymnasium_id }).then((res) => {
					if (res.data.code == 1) {
						_this.setData({ content: '奖励卡核销成功' })
					} else {
						_this.setData({ content: res.data.msg })
					}
				})
			} else if (gymnasium.gymnasium.status == 2) {
				Dialog.alert({ message: '你的场馆被禁用' }).then(() => {
					wx.switchTab({ url: '/pages/personal/my/index' })
				})
			} else if (gymnasium.gymnasium.status == 3) {
				Dialog.alert({ message: '你的场馆被驳回' }).then(() => {
					wx.switchTab({ url: '/pages/personal/my/index' })
				})
			}
		} else if (gymnasium.staff) {
			if (gymnasium.staff.status == 0) {
				Dialog.alert({ message: '你的店员身份正在审核中' }).then(() => {
					wx.switchTab({ url: '/pages/personal/my/index' })
				})
			} else if (gymnasium.staff.status == 1) {
				var order_id = _this.data.order_id
				var gymnasium_id = gymnasium.staff.gymnasium_id
				var type = _this.data.type
				ajax.postRequest('/Order/verification', { reward_id: order_id, type, gymnasium_id }).then((res) => {
					if (res.data.code == 1) {
						_this.setData({ content: '奖励卡核销成功' })
					} else {
						_this.setData({ content: res.data.msg })
					}
				})
			} else if (gymnasium.staff.status == 2) {
				Dialog.alert({ message: '你的店员身份被禁用' }).then(() => {
					wx.switchTab({ url: '/pages/personal/my/index' })
				})
			} else if (gymnasium.staff.status == 3) {
				Dialog.alert({ message: '你的店员身份被驳回' }).then(() => {
					wx.switchTab({ url: '/pages/personal/my/index' })
				})
			}
		}
	},
})